package Util::Store::StoreCreate;
use Image::Magick;
use Moose;
use namespace::autoclean;
use Image::Magick;
use File::Basename;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub createTaobaoStore {
	my ($self, $user_id, $name, $url, $imageUrl, $bulletin, $description, $comm_id, $location, $category, $source) = @_;

        my $store = $self->schema->resultset('Store')->create({
                        name => $name,
                        url => $url,
                        bulletin => $bulletin,
                        description => $description,
                        image => $imageUrl,
                        comm_item_id => $comm_id,
			picked => 'G',
                        location => $location});

        $store->add_to_stores_category({category_id => $category});
        $store->add_to_stores_source({source_id => $source});
        $store->add_to_user_stores({user_id => $user_id, role => 'C'});

	return $store;
}

sub update {
	my ($self, $static, $user_id, $upload, $name, $bulletin, $description, $comm_id, $location, $category, $price, $threshhold_num, $store_id) = @_;

	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

	if($upload){
		my($filename, $directories, $suffix) = fileparse($upload->filename, qr/\.[^.]*/);
		$f_suffix = $suffix;
		my $rand = new String::Random;
		$t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
		$year = 1900 + $year;
		$mon = 1 + $mon;
		my $dir = "/$year/$mon/$mday";

		$t_filename = $t_filename . $suffix;
		$tmp_target = "/tmp/$dir/$t_filename";

		system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
		system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
		system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
		$small_image = "$static/small/$dir/$t_filename";
		$big_image = "$static/big/$dir/$t_filename";
		$t_filename = "$dir/$t_filename";

		unless ( $upload->link_to($tmp_target) || $upload->copy_to($tmp_target) ) {
			die( "Failed to copy '$filename' to '$tmp_target': $!" );
		}

		my $image_small = Image::Magick->new;
		$image_small->read($tmp_target);

		$image_small->Set( Gravity => 'Center' );
		$image_small->Resize( geometry => '130x98' );
		$image_small->Extent( geometry => '130x98' );
		$image_small->Write($small_image);

		my $image_big= Image::Magick->new;
		$image_big->read($tmp_target);

		$image_big->Set( Gravity => 'Center' );
		$image_big->Resize( geometry => '540x411' );
		$image_big->Extent( geometry => '540x411' );
                $image_big->Write($big_image);

        } else {
                $t_filename = "store/default.jpg";
        }

	my $store= $self->schema->resultset('Store')->find($store_id);
        $store->update({
                        name => $name,
                        threshhold_num => $threshhold_num,
                        price => $price,
                        bulletin => $bulletin,
                        description => $description,
                        comm_item_id => $comm_id,
                        location => $location});
	if($upload){
		$store->update({image => $t_filename});
	}
	foreach($store->stores_category){
		$_->delete();
	}
        $store->add_to_stores_category({category_id => $category});

	return $store;
}
sub create {
	my ($self, $static, $user_id, $upload, $name, $bulletin, $description, $comm_id, $location, $category, $price, $threshhold_num) = @_;

	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

	if($upload){
		my($filename, $directories, $suffix) = fileparse($upload->filename, qr/\.[^.]*/);
		$f_suffix = $suffix;
		my $rand = new String::Random;
		$t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
		$year = 1900 + $year;
		$mon = 1 + $mon;
		my $dir = "/$year/$mon/$mday";

		$t_filename = $t_filename . $suffix;
		$tmp_target = "/tmp/$dir/$t_filename";

		system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
		system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
		system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
		$small_image = "$static/small/$dir/$t_filename";
		$big_image = "$static/big/$dir/$t_filename";
		$t_filename = "$dir/$t_filename";

		unless ( $upload->link_to($tmp_target) || $upload->copy_to($tmp_target) ) {
			die( "Failed to copy '$filename' to '$tmp_target': $!" );
		}

		my $image_small = Image::Magick->new;
		$image_small->read($tmp_target);

		$image_small->Set( Gravity => 'Center' );
		$image_small->Resize( geometry => '130x98' );
		$image_small->Extent( geometry => '130x98' );
		$image_small->Write($small_image);

		my $image_big= Image::Magick->new;
		$image_big->read($tmp_target);

		$image_big->Set( Gravity => 'Center' );
		$image_big->Resize( geometry => '540x411' );
		$image_big->Extent( geometry => '540x411' );
                $image_big->Write($big_image);

        } else {
                $t_filename = "store/default.jpg";
        }

        my $store = $self->schema->resultset('Store')->create({
                        name => $name,
                        threshhold_num => $threshhold_num,
                        price => $price,
                        bulletin => $bulletin,
                        description => $description,
                        image => $t_filename,
                        comm_item_id => $comm_id,
                        location => $location});

        $store->add_to_stores_category({category_id => $category});
        $store->add_to_user_stores({user_id => $user_id, role => 'C'});

	return $store;
}

sub imgUpload {
	my ($self, $static, $upload) = @_;

	my ($t_filename, $tmp_target, $big_image, $f_suffix);

	my($filename, $directories, $suffix) = fileparse($upload->filename, qr/\.[^.]*/);
	$f_suffix = $suffix;
	my $rand = new String::Random;
	$t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	$year = 1900 + $year;
	$mon = 1 + $mon;
	my $dir = "/$year/$mon/$mday";

	$t_filename = $t_filename . $suffix;
	$tmp_target = "/tmp/$dir/$t_filename";
	my $return_value =  "/$dir/$t_filename";

	system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
	system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
	$big_image = "$static/big/$dir/$t_filename";
	$t_filename = "$dir/$t_filename";

	unless ( $upload->link_to($tmp_target) || $upload->copy_to($tmp_target) ) {
		die( "Failed to copy '$filename' to '$tmp_target': $!" );
	}

	my $image_big= Image::Magick->new;
	my($width, $height, $size, $format) = $image_big->Ping($tmp_target);
	if($width <= 540) {
		system("mv $tmp_target $big_image");
	} else {
		my $r = 540 * $height;
		my $new_width = $r/$width;
		$image_big->read($tmp_target);
		$image_big->Set( Gravity => 'Center' );
		$image_big->Resize( geometry => "540x$new_width" );
		$image_big->Extent( geometry => "540x$new_width" );
		$image_big->Write($big_image);
	}

	return $return_value;
}

__PACKAGE__->meta->make_immutable;

1;
